package com.kucun.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.kucun.bean.Billmaster;
import com.kucun.bean.Goods;
import com.kucun.bean.Page;
import com.kucun.bean.Users;
import com.kucun.service.Billmasterservice;
import com.kucun.util.GsonUtils;
import com.kucun.util.UID;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


@Controller
@RequestMapping("/billmaster")
public class BillmasterController {

	@Autowired
	private Billmasterservice billmasterservice;
	
	/**
	 * 查询入库单数据
	 * @param request
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping("/getallbillmaster.html")
	public String getAllBillMaster(int rows,int page,HttpServletRequest request,HttpServletResponse response) throws IOException{
		Users user=(Users)request.getSession().getAttribute("user");
		String field=request.getParameter("field");
        String searchertext=request.getParameter("searchertext");
        if("".equals(searchertext)||searchertext==null){
			 
        }else{
        	searchertext = URLDecoder.decode(searchertext, "utf-8");
        } 
		Page page1=new Page();
		page1.setI(rows*(page-1));
		page1.setRows(rows);
		page1.setFiled(field);
		page1.setSearchertext(searchertext);
		page1.setUserid(user.getUserid());
		Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map  
		List<Billmaster> list = billmasterservice.findAll(page1);	
		jsonMap.put("total",billmasterservice.getCount(page1).getTotal());//total键 存放总记录数，必须的  
        jsonMap.put("rows",list);//rows键 存放每页记录 list  
//        JSONObject jsonObject = JSONObject.fromObject(jsonMap);//格式化result   一定要是JSONObject  
        PrintWriter pw = response.getWriter();
        Object jsonFromBean=GsonUtils.ConvertJsonDateToDate(jsonMap,"yyyy-MM-dd HH:mm:ss");
        pw.write(jsonFromBean.toString());
		return null;
	}
	
	@RequestMapping("/querybillmaster.html")
	public String querybillmaster(Billmaster billmaster,int rows,int page,int filed,HttpServletRequest request,HttpServletResponse response) throws IOException{
		Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map  
		List<Billmaster> list = billmasterservice.query(billmaster,rows*(page-1),rows);	
		jsonMap.put("total",billmasterservice.queryCount(billmaster).getTotal());//total键 存放总记录数，必须的  
        jsonMap.put("rows",list);//rows键 存放每页记录 list  
        JSONObject jsonObject = JSONObject.fromObject(jsonMap);//格式化result   一定要是JSONObject  
        PrintWriter pw = response.getWriter();
        Object jsonFromBean=GsonUtils.ConvertJsonDateToDate(jsonObject,"yyyy-MM-dd HH:mm:ss");
        pw.write(jsonFromBean.toString());
		return null;
	}

	/**
	 * @param user
	 * @param request
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping("/updateuser.html")
	public void updateUser(Billmaster billmaster,HttpServletRequest request,HttpServletResponse response) throws IOException{
	    PrintWriter out = response.getWriter();
	    if (billmaster.getId()!=null) {
	    	if(billmasterservice.update(billmaster)){
	  			out.write("ok");
	  		}else{
	  			out.write("error");
	  		}
		}else{
			if(billmasterservice.save(billmaster)){
	  			out.write("ok");
	  		}else{
	  			out.write("error");
	  		}
		}
	  
	}
	
	@RequestMapping("/getid.html")
	public void getno(HttpServletRequest request,HttpServletResponse response) throws IOException{
		Integer id=billmasterservice.getid();
		String result = "{\"result\":\""+id+"\"}";
		PrintWriter out = response.getWriter();
	    out.write(result);
	}
	/**查询入库单主表数据
	 * @param id
	 * @param request
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping("/getbillmaster.html")
	public String getbillmaster(int cs,HttpServletRequest request,HttpServletResponse response) throws IOException{
		List<Billmaster> list = new ArrayList<Billmaster>();
		Billmaster billmaster=billmasterservice.findById(cs);
		list.add(billmaster);
		JSONArray jsonObject = JSONArray.fromObject(list);//格式化result   一定要是JSONObject  
        PrintWriter pw = response.getWriter();
        System.out.println(jsonObject.toString());
        pw.write(jsonObject.toString());
		return null;
	}
	/**
	 * @param id
	 * @param request
	 * @param response
	 */
	@RequestMapping("/delet.html")
	public void delUser(String id,HttpServletRequest request,HttpServletResponse response){
		String result = "{\"result\":\"error\"}";
		String string=id;
		if(billmasterservice.delete(string)){
			result = "{\"result\":\"success\"}";
		}
		response.setContentType("application/json");
		try {
			PrintWriter out = response.getWriter();
			out.write(result);
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
	
	@RequestMapping("/outboundorder.html")
	public String getAllUser(int rows,int page,HttpServletRequest request,HttpServletResponse response) throws IOException{
        String field=request.getParameter("field");
        String searchertext=request.getParameter("searchertext");
        Users user=(Users)request.getSession().getAttribute("user");
        if("".equals(searchertext)||searchertext==null){
			 
        }else{
        	searchertext = URLDecoder.decode(searchertext, "utf-8");
        } 
		Page page1=new Page();
		page1.setI(rows*(page-1));
		page1.setRows(rows);
		page1.setFiled(field);
		page1.setSearchertext(searchertext);
		page1.setUserid(user.getUserid());
		Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map  
		List<Billmaster> list = billmasterservice.findAllOutofOrder(page1);	
		jsonMap.put("total",billmasterservice.getCountOfOutOrder(page1).getTotal());//total键 存放总记录数，必须的  
        jsonMap.put("rows",list);//rows键 存放每页记录 list    
        PrintWriter pw = response.getWriter();
        Object jsonFromBean=GsonUtils.ConvertJsonDateToDate(jsonMap,"yyyy-MM-dd HH:mm:ss");
        System.out.println(jsonFromBean.toString());
        pw.write(jsonFromBean.toString());		
        return null;
	}
}
